$(function(){
	initTableField();
});

function initTableField(){
	var table_name = $.trim($("#table_name").val());
	$.ajax({
        url: base + "/dataCenter/businessdata/findFromFieldList/"+table_name,
        async: true,
        data: {},
        success: function (data, textStatus, jqXHR) {
        	if(data != null && data.length > 0){
	        	$.each(data,function(index,value){
	        		if("id"!=data[index].field_name && "project_id"!=data[index].field_name){
	        			var field_name = data[index].field_name;
	        			var field_comment = data[index].field_comment;
	        			var field_length = data[index].field_length;
	        			var field_type = data[index].field_type;
	        			var is_null = data[index].is_null;
	        			var field_default = data[index].field_default;
	        			alert(field_type);
	        			var appendHtml = ' <tr>'
	                        + ' <td>'+$("#from_field_table").children().length+'</td>'
	                        + ' <td><input type="checkbox" name="field_select"></td>'
	                        + ' <td><input type="text" style="width: 100px;height: 30px;"  name="field_name" value="'+field_name+'"></td>'
	                        + ' <td><input type="text" style="width: 100px;height: 30px;"  name="field_comment" value="'+field_comment+'"></td>'
	                        + ' <td><input type="text" style="width: 80px;height: 30px;"  name="field_length" value="'+field_length+'"></td>'
	                        + ' <td><input type="text" style="width: 80px;height: 30px;" name="field_default" value="'+field_default+'"></td>'
	                        + ' <td>'
	                        + ' <select class="selectpicker show-tick form-control" style="height: 30px;font-size: 12px;" name="field_type">';
	                        if("varchar"==field_type){
	                        	appendHtml += '<option value="varchar" selected="selected">varchar</option><option value="int">int</option><option value="double">double</option>';
	                        }else if("date"==field_type){
	                        	appendHtml += '<option value="varchar">varchar</option><option value="int" selected="selected">int</option><option value="double">double</option>';
	                        }else if("decimal"==field_type){
	                        	appendHtml += '<option value="varchar">varchar</option><option value="int">int</option><option value="double" selected="selected">double</option>';
	                        }
	                        appendHtml += '</select>'
	                        appendHtml += '</td>'
	                        + '<td><input type="checkbox" name="is_id" disabled="true"></td>';
	                        if("NO"==is_null){
	                        	//非空
	                        	appendHtml += '<td><input type="checkbox" name="is_null"></td>';
	                        }else if("YES"==is_null){
	                        	appendHtml += '<td><input type="checkbox" checked="checked" name="is_null"></td>';
	                        }
	                        appendHtml += '</tr>';
	                    $("#from_field_table").append(appendHtml); 
	        		}
	        	});
        	}
        },
        error: function () {
        }
    });
}

//添加字段
var appendField = function(){
	//序号自增
	var obj = $("#tpl").children().children().children("tr").children("td");
	obj.each(function(children_index,value){
			if(children_index=='0'){
			$(this).text($("#from_field_table").children().length);
		} 
	});
	var html = '<tr>'+obj.parent().html()+'</tr>';
	$("#from_field_table").append(html); 
}


//删除字段
var delField = function(){
	var select = $("input[name='field_select']:checked");
	if(select.length > 0){
		$.each(select,function(index,value){
			var remove_obj = $(this).parent().parent();
			remove_obj.remove();
		});
	}else{
		alert("请选择栏位!");
	}
	//重新排序
    $("#from_field_table").children().each(function(parent_index,value){
	if(parent_index > 0){
		$(this).children().each(function(children_index,value){
			if(children_index=='0'){
				$(this).text(parent_index);
			}
		});
	}
    }); 
}

//初始化状态
function initState(){
 	   var data_obj = $("#from_field_table").children("tr");
	   $.each(data_obj,function(index,value){
		   if(index>1){
 			   $.each($(this).children("td"),function(children_index,value){
				   if(children_index > 1){
					   var obj_name = $(this).children().attr("name");
					   var obj_val = $(this).children().val();
					   if("field_type"==obj_name && "double"==obj_val ){
						   var field_length_obj = $(this).parent().find("td input[name='field_length']");
						   field_length_obj.val('');
						   field_length_obj.attr("disabled",true);
					   }
				   }
			   }); 
		   }
	   }); 
} 

$("body").delegate("select","change",function(){
var field_type = $.trim($(this).val());
if("int"==field_type){
	var field_length_obj = $(this).parent().parent().find("td input[name='field_length']");
	field_length_obj.attr("disabled",false);
}else if("double"==field_type){
	//double类型没有字段长度
	var field_length_obj = $(this).parent().parent().find("td input[name='field_length']");
	field_length_obj.val('');
	field_length_obj.attr("disabled",true);
}else if("text"==field_type){
	//text类型没有字段长度
	var field_length_obj = $(this).parent().parent().find("td input[name='field_length']");
	field_length_obj.val('');
	field_length_obj.attr("disabled",true);
}else if("varchar"==field_type){
	var field_length_obj = $(this).parent().parent().find("td input[name='field_length']");
	field_length_obj.attr("disabled",false);
}
}); 